package cn.jiyun.service.impl;

import cn.jiyun.fegin.FenLeiFegin;
import cn.jiyun.fegin.StatesFegin;
import cn.jiyun.mapper.FoodsMapper;
import cn.jiyun.pojo.FenLei;
import cn.jiyun.pojo.Foods;
import cn.jiyun.pojo.States;
import cn.jiyun.service.FoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FoodsServiceImpl implements FoodsService {

    @Autowired
    private FoodsMapper foodsMapper;
    @Autowired
    private StatesFegin statesFegin;
    @Autowired
    private FenLeiFegin fenLeiFegin;

    @Override
    public List<Foods> findAll(Foods foods1) {
//         食物表数据
        List<Foods> foodsList = foodsMapper.findAll(foods1);
//        状态表数据
        List<States> statesList = statesFegin.findAll();
//        分类表数据
        List<FenLei> fenLeiList = fenLeiFegin.findAll();
//      状态表的map集合
        Map<Integer,States> statesMap = new HashMap<Integer,States>();
//        把状态表的数据放到map集合中
        statesList.forEach(states -> {
            statesMap.put(states.getId(),states);
        });
//        把状态表数据存入到foods中
        foodsList.forEach(foods -> {
            foods.setSname(statesMap.get(foods.getSid()).getName());
        });

        Map<Integer,FenLei> fenLeiMap = new HashMap<Integer,FenLei>();
        fenLeiList.forEach(fenLei -> {
            fenLeiMap.put(fenLei.getId(),fenLei);
        });
        foodsList.forEach(foods -> {
            foods.setFname(fenLeiMap.get(foods.getFid()).getName());
        });
        return foodsList;


    }

    @Override
    public void add(Foods foods) {
        foodsMapper.add(foods);
    }

    @Override
    public void update(Foods foods) {
        foodsMapper.update(foods);
    }

    @Override
    public void delete(Integer id) {
        foodsMapper.delete(id);
    }


}
